<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<!--<el-form-item>-->
					<!--<el-input  placeholder="玩家ID"></el-input>-->
				<!--</el-form-item>-->
				<!--<el-form-item>-->
					<!--<el-button type="primary" v-on:click="getUsers">查询</el-button>-->
				<!--</el-form-item>-->
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading"  style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="userId" label="玩家ID" width="200">
			</el-table-column>
			<el-table-column label="操作" min-width="200">
				<template slot-scope="scope">
					<el-button type="danger" size="small" @click="delUser(scope.row.userId)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form label-width="80px" >
				<el-form-item label="玩家ID" prop="name">
					<el-input v-model="addUserId" auto-complete="off" type="number"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				users: [],
				default:[],
				total: 0,
				page: 1,
				listLoading: false,
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				//新增界面数据
				addUserId:''

			}
		},
		methods: {
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers();
			},
			//获取用户列表
			getUsers() {
				this.listLoading = true;
				this.$axios.get('/hundred-master-whitelist',{},this.global().token).then((res)=>{
				    if(res.status == 200){
                        this.listLoading = false;
                        this.default = res.data
                        this.users = []
                        this.default.forEach((val)=>{
                            let newData = new Object();
                            newData.userId = val
                            this.users.push(newData)
                        })
					}
				})
			},
			//删除
            delUser(userId) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
                    Array.prototype.indexOf = function(val) {
                        for (var i = 0; i < this.length; i++) {
                            if (this[i] == val) return i;
                        }
                        return -1;
                    };
                    Array.prototype.remove = function(val) {
                        var index = this.indexOf(val);
                        if (index > -1) {
                            this.splice(index, 1);
                        }
                    };
                    this.default.remove(userId);
                    this.$axios.patch('/hundred-master-whitelist',this.default,this.global().token).then((res)=>{
                        if(res.status == 200){
                            this.$message({message: '删除成功', type: 'success'});
                            this.getUsers()
                        }
                    })
				}).catch(() => {

				});
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
			},
			//新增
			addSubmit: function () {
			    if(this.addUserId == ''){
                    this.$message({message: '请输入玩家ID', type: 'error'});
                    return
				}
				this.default.push(parseInt(this.addUserId))
				this.$axios.patch('/hundred-master-whitelist',this.default,this.global().token).then((res)=>{
                    this.addFormVisible = false;
				    if(res.status == 200){
                        this.$message({message: '添加成功', type: 'success'});
                        this.getUsers()
                        this.addUserId = ''
					}
				})
			},
		},
		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>